﻿#include "QMainFormTitleBar.h"

#ifdef Q_OS_WIN
#pragma comment(lib, "user32.lib")
#include <qt_windows.h>
#endif

QMainFormTitleBar::QMainFormTitleBar(QWidget *parent) : QWidget(parent)
{
    installEventFilter(parent);

    StyleSheetHelper = new QStyleSheetHelper();

    TitleIconTextHorizontalLayout = new QHBoxLayout(this);
    TitleIconTextHorizontalLayout->setSpacing(0);
    TitleIconTextHorizontalLayout->setContentsMargins(11, 11, 11, 11);
    TitleIconTextHorizontalLayout->setObjectName(QStringLiteral("TitleIconTextHorizontalLayout"));
    TitleIconTextHorizontalLayout->setContentsMargins(0, 0, 0, 0);

    FormTitleIconLabel = new QLabel(this);
    QSizePolicy TitleIconLabelSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
    TitleIconLabelSizePolicy.setHorizontalStretch(0);
    TitleIconLabelSizePolicy.setVerticalStretch(0);
    TitleIconLabelSizePolicy.setHeightForWidth(FormTitleIconLabel->sizePolicy().hasHeightForWidth());
    FormTitleIconLabel->setSizePolicy(TitleIconLabelSizePolicy);
    FormTitleIconLabel->setMinimumSize(QSize(30, 0));
    FormTitleIconLabel->setAlignment(Qt::AlignCenter);
    FormTitleIconLabel->setScaledContents(true);
    TitleIconTextHorizontalLayout->addWidget(FormTitleIconLabel);

    FormTitleTextLabel = new QLabel(this);
    FormTitleTextLabel->setObjectName(tr("whiteLabel"));
    FormTitleTextLabel->setFont(parent->font());
    QSizePolicy TitleTextSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    TitleTextSizePolicy.setHorizontalStretch(0);
    TitleTextSizePolicy.setVerticalStretch(0);
    TitleTextSizePolicy.setHeightForWidth(FormTitleTextLabel->sizePolicy().hasHeightForWidth());
    FormTitleTextLabel->setSizePolicy(TitleTextSizePolicy);
    FormTitleTextLabel->setStyleSheet(QStringLiteral(""));
    FormTitleTextLabel->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
    TitleIconTextHorizontalLayout->addWidget(FormTitleTextLabel);

    /** 最小化, 最大化, 关闭*/
    TitleButtonWidget = new QWidget(this);
    TitleButtonWidget->setObjectName(QStringLiteral("TitleButtonMenu"));
    QSizePolicy TitleButtonSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
    TitleButtonSizePolicy.setHorizontalStretch(0);
    TitleButtonSizePolicy.setVerticalStretch(0);
    TitleIconLabelSizePolicy.setHeightForWidth(TitleButtonWidget->sizePolicy().hasHeightForWidth());
    TitleButtonWidget->setSizePolicy(TitleIconLabelSizePolicy);

    TitleButtonHorizontalLayout = new QHBoxLayout(TitleButtonWidget);
    TitleButtonHorizontalLayout->setSpacing(0);
    TitleButtonHorizontalLayout->setContentsMargins(11, 11, 11, 11);
    TitleButtonHorizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
    TitleButtonHorizontalLayout->setContentsMargins(0, 0, 0, 0);

    QSizePolicy ButtonSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
    ButtonSizePolicy.setHorizontalStretch(0);
    ButtonSizePolicy.setVerticalStretch(0);

    FormMinimizeButton = new QPushButton(TitleButtonWidget);
    FormMinimizeButton->setObjectName(tr("minimizeButton"));
    ButtonSizePolicy.setHeightForWidth(FormMinimizeButton->sizePolicy().hasHeightForWidth());
    FormMinimizeButton->setSizePolicy(ButtonSizePolicy);
    FormMinimizeButton->setMinimumSize(QSize(40, 0));
    FormMinimizeButton->setCursor(QCursor(Qt::ArrowCursor));
    FormMinimizeButton->setFocusPolicy(Qt::NoFocus);
    FormMinimizeButton->setFlat(true);
    StyleSheetHelper->SetIcon(FormMinimizeButton, QChar(0xf068));
    TitleButtonHorizontalLayout->addWidget(FormMinimizeButton);

    FormMaximizeButton = new QPushButton(TitleButtonWidget);
    FormMaximizeButton->setObjectName(tr("maximizeButton"));
    ButtonSizePolicy.setHeightForWidth(FormMaximizeButton->sizePolicy().hasHeightForWidth());
    FormMaximizeButton->setSizePolicy(ButtonSizePolicy);
    FormMaximizeButton->setMinimumSize(QSize(40, 0));
    FormMaximizeButton->setCursor(QCursor(Qt::ArrowCursor));
    FormMaximizeButton->setFocusPolicy(Qt::NoFocus);
    FormMaximizeButton->setFlat(true);
    StyleSheetHelper->SetIcon(FormMaximizeButton, QChar(0xf096));
    TitleButtonHorizontalLayout->addWidget(FormMaximizeButton);

    FormCloseButton = new QPushButton(TitleButtonWidget);
    FormCloseButton->setObjectName(tr("closeButton"));
    ButtonSizePolicy.setHeightForWidth(FormCloseButton->sizePolicy().hasHeightForWidth());
    FormCloseButton->setSizePolicy(ButtonSizePolicy);
    FormCloseButton->setMinimumSize(QSize(40, 0));
    FormCloseButton->setCursor(QCursor(Qt::ArrowCursor));
    FormCloseButton->setFocusPolicy(Qt::NoFocus);
    FormCloseButton->setFlat(true);
    StyleSheetHelper->SetIcon(FormCloseButton, QChar(0xf00d));
    TitleButtonHorizontalLayout->addWidget(FormCloseButton);

    TitleIconTextHorizontalLayout->addWidget(TitleButtonWidget);

#ifdef ENGLISH
    FormMinimizeButton->setToolTip(tr("Minimize"));
    FormMaximizeButton->setToolTip(tr("Maximize"));
    FormCloseButton->setToolTip(tr("Close"));
#endif

#ifdef  CHINESE
    FormMinimizeButton->setToolTip(tr("最小化"));
    FormMaximizeButton->setToolTip(tr("最大化"));
    FormCloseButton->setToolTip(tr("关闭"));
#endif

    connect(FormMinimizeButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
    connect(FormMaximizeButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
    connect(FormCloseButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
}

QMainFormTitleBar::~QMainFormTitleBar()
{
    delete FormTitleIconLabel;
    delete FormTitleTextLabel;
    delete FormMinimizeButton;
    delete FormMaximizeButton;
    delete FormCloseButton;
    delete StyleSheetHelper;
    delete TitleIconTextHorizontalLayout;
    delete TitleButtonHorizontalLayout;
}

void QMainFormTitleBar::SetTitleText(QString TitleText)
{
    FormTitleTextLabel->setText(TitleText);
}

void QMainFormTitleBar::SetTitleIcon(QIcon TitleIcon)
{
    FormTitleIconLabel->setPixmap(TitleIcon.pixmap(FormTitleIconLabel->size()));
}

void QMainFormTitleBar::SetTitleIcon(QChar Descriptor)
{
    StyleSheetHelper->SetIcon(FormTitleIconLabel, Descriptor);
}

void QMainFormTitleBar::SetMinimizeButtonIcon(QChar Descriptor)
{
    StyleSheetHelper->SetIcon(FormMinimizeButton, Descriptor);
}

void QMainFormTitleBar::SetMaximizeButtonIcon(QChar Descriptor)
{
    StyleSheetHelper->SetIcon(FormMaximizeButton, Descriptor);
}

void QMainFormTitleBar::SetCloseButtonIcon(QChar Descriptor)
{
    StyleSheetHelper->SetIcon(FormCloseButton, Descriptor);
}

void QMainFormTitleBar::resizeEvent(QResizeEvent *event)
{

}

void QMainFormTitleBar::paintEvent(QPaintEvent *paintEvent)
{
    if(paintEvent != NULL) {

    }

    QStyleOption StyleOption;
    StyleOption.init(this);
    QPainter Painter(this);
    style()->drawPrimitive(QStyle::PE_Widget, &StyleOption, &Painter, this);
}

void QMainFormTitleBar::onClicked()
{
    QPushButton* OnClickedPushButton = qobject_cast<QPushButton *>(sender());
    if (OnClickedPushButton == FormMinimizeButton) {
        emit ShowMinimized();
    } else if (OnClickedPushButton == FormMaximizeButton) {
        emit ShowMaximized();
    } else if (OnClickedPushButton == FormCloseButton) {
        emit CloseForm();
    }
}

void QMainFormTitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
    emit TitleMouseDoubleClickEvent(event);
}

void QMainFormTitleBar::mouseMoveEvent(QMouseEvent *event)
{
    emit TitleMouseMoveEvent(event);
}

void QMainFormTitleBar::mousePressEvent(QMouseEvent *event)
{
    emit TitleMousePressEvent(event);
}

void QMainFormTitleBar::mouseReleaseEvent(QMouseEvent *event)
{
    emit TitleMouseReleaseEvent(event);
}

void QMainFormTitleBar::moveEvent(QMoveEvent *event)
{
    Q_UNUSED(event);
}

bool QMainFormTitleBar::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    return QWidget::nativeEvent(eventType, message, result);
}

bool QMainFormTitleBar::eventFilter(QObject *watched, QEvent *event)
{
    return QWidget::eventFilter(watched, event);
}
